<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">注册</h3>
      </div>

      <!-- 邮箱 -->
      <p>
        <el-form-item prop="email">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="email"
            v-model="loginForm.email"
            placeholder="请输入邮箱"
            name="email"
            type="text"
            tabindex="5"
          />
        </el-form-item>
        <el-button :disabled="isDisable" size="small" round @click="sendCode">
          {{ buttonText }}
        </el-button>
      </p>

      <!-- 验证码 -->
      <el-form-item prop="verifycode">
        <span class="svg-container">
          <i class="fas fa-shield-alt"></i>
        </span>

        <el-input
          ref="verifycode"
          v-model="loginForm.verifyCode"
          placeholder="请输入验证码"
          name="verifycode"
          type="text"
          tabindex="6"
          style="width: 70%"
        />
      </el-form-item>

      <el-tooltip
        v-model="capsTooltip"
        content="Caps lock is On"
        placement="right"
        manual
      >
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="密码"
            tabindex="2"
            autocomplete="on"
            @keyup.native="checkCapslock"
            @blur="capsTooltip = false"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon
              :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
            />
          </span>
        </el-form-item>
      </el-tooltip>
      <el-tooltip
        v-model="recapsTooltip"
        content="Caps lock is On"
        placement="right"
        manual
      >
        <el-form-item prop="repassword">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="repasswordType"
            ref="repassword"
            v-model="loginForm.repassword"
            :type="repasswordType"
            placeholder="确认密码"
            tabindex="3"
            autocomplete="on"
            @keyup.native="recheckCapslock"
            @blur="recapsTooltip = false"
          />
          <span class="show-pwd" @click="reshowPwd">
            <svg-icon
              :icon-class="repasswordType === 'password' ? 'eye' : 'eye-open'"
            />
          </span>
        </el-form-item>
      </el-tooltip>

      <el-button
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        @click.native.prevent="userRegister"
        >注册</el-button
      >
      <p class="tips">
        已有帐号？<a href="/login" style="color: rgb(80, 248, 8)">点击登录</a>
      </p>
    </el-form>
    <!-- 人机验证 -->
    <Vcode :show="isShow" @success="onSuccess()" @close="onClose()" />
  </div>
</template>
  
  <script>
import { sendCode, register } from "@/api/user";
import Vcode from "vue-puzzle-vcode"; //人机验证
export default {
  name: "Register",
  components: { Vcode },
  data() {
    return {
      isShow: false, //人机验证是否显示
      loginForm: {
        email: "",
        verifyCode: "", //验证码
        password: "",
      },
      isDisable: false, //发送验证码这个按钮是否可点
      buttonText: "发送验证码",
      capsTooltip: false,
      passwordType: "password",
      recapsTooltip: false,
      repasswordType: "password",
      loginRules: {
        // 其他校验规则
        repassword: [
          { required: true, message: "请再次输入密码", trigger: "blur" },
          { validator: this.validateRepassword, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    //发送验证码
    sendCode() {
      if (this.loginForm.email == "") {
        this.$message.error("邮箱不能为空！");
      } else {
        this.isDisable = true; //设置为不可点
        this.countDown(60);
        console.log(this.loginForm.email);
        sendCode(this.loginForm.email)
          .then((res) => {
            this.isDisable = false;
            console.log("验证码：", res.data.code);
          })
          .catch((err) => {
            this.$message.error(err.data);
            this.isDisable = false;
            this.buttonText = "发送验证码";
          });
      }
    },
    //60s倒计时
    countDown(seconds) {
      this.buttonText = `${seconds} 秒后重新发送`;
      const timer = setInterval(() => {
        seconds--;
        if (seconds === 0) {
          clearInterval(timer);
          this.buttonText = "发送验证码";
          this.isDisable = false; // 按钮可点击
        } else {
          this.buttonText = `${seconds} 秒后重新发送`;
        }
      }, 1000);
    },
    // 验证两次输入密码是否一致的方法
    validateRepassword(rule, value, callback) {
      if (value === this.loginForm.password) {
        callback();
      } else {
        callback(new Error("两次输入的密码不一致"));
      }
    },
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    checkCapslock(e) {
      const { key } = e;
      this.capsTooltip = key && key.length === 1 && key >= "A" && key <= "Z";
    },
    reshowPwd() {
      if (this.repasswordType === "password") {
        this.repasswordType = "";
      } else {
        this.repasswordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.repassword.focus();
      });
    },
    recheckCapslock(e) {
      const { key } = e;
      this.recapsTooltip = key && key.length === 1 && key >= "A" && key <= "Z";
    },
    reload() {
      //  "路径?t=" + new Date() ，提供一个t变量，用于唯一标识每一次访问路径
      // this.verifycodeImg = `http://localhost:10010/v2/user-service/verifycode/${this.loginForm.username}?t=` + new Date().getTime()
      this.rerifycodeImg = `${process.env.VUE_APP_BASE_API}/use/verifycode/${
        this.loginForm.username
      }?t=${new Date().getTime()}`;
    },
    //关闭验证
    // 关闭滑块验证后提示用户取消验证
    onClose() {
      this.isShow = false;
      this.$message({
        message: "取消验证！",
        type: "warning",
      });
    },
    //人机验证（登录请求在这里发送）
    //滑块验证成功
    onSuccess(msg) {
      this.isShow = false;
      register(this.loginForm).then((res) => {
        this.$message.success("注册成功，请登录！");
        this.$router.push("/login");
      });
    },
    //注册
    async userRegister() {
      if (
        this.loginForm.email === "" ||
        this.loginForm.verifyCode === "" ||
        this.loginForm.password === "" ||
        this.loginForm.repassword === ""
      ) {
        this.$message.error("请完整填写信息");
      } else {
        this.isShow = true;
      }
    },
  },
};
</script>
  
<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
// $bg:#2d3a4b;
// $dark_gray:#889aa4;
// $light_gray:#eee;
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
.login-container {
  width: 100%;
  height: 100%;
  background-image: url("../../assets/register.jpg");
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: -50px auto 0px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
  
  